﻿using System;
using System.Reflection;
using System.Xml;
using Notoric.Model.Internal;
using Notoric.Model.Packaging;
using Notoric.Model.Validation;

namespace Notoric.Model.Elements.CWNotation
{
    /// <summary>
    /// Describes a connector of two staves.
    /// </summary>
    public class StaffConnectorDescriptor : ModelElement
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="StaffConnectorDescriptor"/> instance.
        /// </summary>
        /// <param name="packagePart">
        /// <para>
        /// Type: <see cref="XmlPackagePart"/>
        /// </para>
        /// <para>
        /// The package part that owns this element.
        /// </para>
        /// </param>
        /// <param name="owner">
        /// <para>
        /// Type: <see cref="ModelElement"/>
        /// </para>
        /// <para>
        /// The model element that directly owns this element.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="packagePart"/> or <paramref name="owner"/> is <see langword="null"/>.
        /// </exception>
        internal StaffConnectorDescriptor(XmlPackagePart packagePart, ModelElement owner)
            : base(packagePart, owner)
        {
            if (owner == null)
                throw new ArgumentNullException("owner");

        }

        /// <summary>
        /// Creates a new <see cref="StaffConnectorDescriptor"/> instance.
        /// </summary>
        /// <param name="packagePart">
        /// <para>
        /// Type: <see cref="XmlPackagePart"/>
        /// </para>
        /// <para>
        /// The package part that owns this element.
        /// </para>
        /// </param>
        /// <param name="owner">
        /// <para>
        /// Type: <see cref="ModelElement"/>
        /// </para>
        /// <para>
        /// The model element that directly owns this element.
        /// </para>
        /// </param>
        /// <param name="element">
        /// <para>
        /// Type: <see cref="XmlElement"/>
        /// </para>
        /// <para>
        /// The XML element that holds the data of this object.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Any of the arguments is <see langword="null"/>.
        /// </exception>
        internal StaffConnectorDescriptor(XmlPackagePart packagePart, ModelElement owner, XmlElement element)
            : base(packagePart, owner, element)
        {
            if (owner == null)
                throw new ArgumentNullException("owner");

            connectorClassAttribute = element.Attributes[PackageVocabulary.ConnectorClassAttribute];
            lowerStaffAttribute = element.Attributes[PackageVocabulary.LowerStaffAttribute];
            upperStaffAttribute = element.Attributes[PackageVocabulary.UpperStaffAttribute];

            var assembly = Assembly.GetExecutingAssembly();
            connectorClass = assembly.GetType(connectorClassAttribute.Value);
        }

        #endregion

        #region ModelElement Members

        /// <inheritdoc/>
        protected override XmlElement CreateObjectElement(XmlDocument document)
        {
            var element = document.CreateElement(
                PackageVocabulary.PackageNamespacePrefix,
                PackageVocabulary.StaffConnectorElementName,
                PackageVocabulary.PackageNamespaceUri
                );

            connectorClassAttribute = document.CreateAttribute(PackageVocabulary.ConnectorClassAttribute);
            lowerStaffAttribute = document.CreateAttribute(PackageVocabulary.LowerStaffAttribute);
            upperStaffAttribute = document.CreateAttribute(PackageVocabulary.UpperStaffAttribute);

            element.Attributes.Append(connectorClassAttribute);
            element.Attributes.Append(lowerStaffAttribute);
            element.Attributes.Append(upperStaffAttribute);

            return element;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the connector class.
        /// </summary>
        /// <value>
        /// <para>
        /// Type: <see cref="Type"/>
        /// </para>
        /// <para>
        /// The type of the connector. It has to be a subclass
        /// of <see cref="StaffConnector"/>.
        /// </para>
        /// </value>
        /// <exception cref="ValidationException">
        /// The <paramref name="value"/> is <see langword="null"/>.<br/>
        /// -or-<br/>
        /// The <paramref name="value"/> is not a subclass of <see cref="StaffConnector"/>
        /// </exception>
        /// TODO: Write the validation rules.
        public Type ConnectorClass
        {
            get { return connectorClass; }
            set
            {
                ValidateAndThrow("ConnectorClass", value);

                connectorClass = value;
                connectorClassAttribute.Value = value.FullName;

                RaisePropertyChanged("ConnectorClass");
            }
        }

        /// <summary>
        /// Gets or sets the lower connected staff.
        /// </summary>
        /// <value>
        /// <para>
        /// Type: <see cref="StaffDescriptor"/>
        /// </para>
        /// <para>
        /// The lower connected staff.
        /// </para>
        /// </value>
        /// <exception cref="ValidationException">
        /// The <paramref name="value"/> is <see langword="null"/>.<br/>
        /// The <paramref name="value"/> does not belong to this part.
        /// </exception>
        /// TODO: Write the validation rules
        public StaffDescriptor LowerStaff
        {
            get 
            {
                if (lowerStaff == null)
                    lowerStaff = PackagePart.Package.IDMap.FindObject<StaffDescriptor>(new Guid(lowerStaffAttribute.Value));
                return lowerStaff; 
            }
            set
            {
                ValidateAndThrow("LowerStaff", value);

                lowerStaff = value;
                lowerStaffAttribute.Value = value.ID.ToString();

                RaisePropertyChanged("LowerStaff");
            }
        }

        /// <summary>
        /// Gets or sets the upper connected staff.
        /// </summary>
        /// <value>
        /// <para>
        /// Type: <see cref="StaffDescriptor"/>
        /// </para>
        /// <para>
        /// The upper connected staff.
        /// </para>
        /// </value>
        /// <exception cref="ValidationException">
        /// The <paramref name="value"/> is <see langword="null"/>.<br/>
        /// The <paramref name="value"/> does not belong to this part.
        /// </exception>
        /// TODO: Write the validation rules
        public StaffDescriptor UpperStaff
        {
            get 
            {
                if (upperStaff == null)
                    upperStaff = PackagePart.Package.IDMap.FindObject<StaffDescriptor>(new Guid(upperStaffAttribute.Value));
                return upperStaff; 
            }
            set
            {
                ValidateAndThrow("UpperStaff", value);

                upperStaff = value;
                upperStaffAttribute.Value = upperStaff.ID.ToString();

                RaisePropertyChanged("UpperStaff");
            }
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the value of the <see cref="ConnectorClass"/> property.
        /// </summary>
        private Type connectorClass;
        /// <summary>
        /// Holds the value of the <see cref="ConnectorClass"/> property.
        /// </summary>
        private XmlAttribute connectorClassAttribute;
        /// <summary>
        /// Holds the value of the <see cref="LowerStaff"/> property.
        /// </summary>
        private StaffDescriptor lowerStaff;
        /// <summary>
        /// Holds the value of the <see cref="LowerStaff"/> property.
        /// </summary>
        private XmlAttribute lowerStaffAttribute;
        /// <summary>
        /// Holds the value of the <see cref="UpperStaff"/> property.
        /// </summary>
        private StaffDescriptor upperStaff;
        /// <summary>
        /// Holds the value of the <see cref="UpperStaff"/> property.
        /// </summary>
        private XmlAttribute upperStaffAttribute;

        #endregion
    }
}
